package com.android.diary;
import org.apkplug.Bundle.BundleListActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class DiaryActivity extends ListActivity {

	Diary diary;
	Cursor cursor;
	private static final int INSERT_ID=Menu.FIRST;
	private static final int DELETE_ID=Menu.FIRST+1;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_list);
        TextView share=(TextView)this.findViewById(R.id.share);
        share.setText("SharedPreferences:"+SharedPreferencesFactory.getInstance(this).getString("test",""));
        diary=new Diary(this);
        diary.open();
//       diary.deleteTable();
        
        
        ShowListView();
        
    }
    private void ShowListView(){
    	cursor=diary.getAllDiary();
    	startManagingCursor(cursor);
    	String[] columns=new String[]{Diary.key_title,Diary.key_created};
    	int[] layoutIds=new int[] {R.id.text1,R.id.created};
    	SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.diary_row,cursor,columns,layoutIds);
    	Log.i("setcontentview","setcontentview");
    	setListAdapter(adapter);

    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		 super.onCreateOptionsMenu(menu);
		 menu.add(0, INSERT_ID, 0, R.string.menu_insert);
		 menu.add(0, DELETE_ID, 0, R.string.menu_delete);
		 return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case INSERT_ID:
			//
			createDiary();
			return true;
		case DELETE_ID:
			diary.deleteDiary(getListView().getSelectedItemId());
			ShowListView();
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;
    private void createDiary(){
    	Intent intent=new Intent(this,DiaryEdit.class);
    	startActivityForResult(intent, ACTIVITY_CREATE);
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		ShowListView();
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Cursor c=cursor;
		c.moveToPosition(position);
		Intent i=new Intent(this,DiaryEdit.class);
		i.putExtra(Diary.key_id,id);
		i.putExtra(Diary.key_title,c.getString(c.getColumnIndexOrThrow(Diary.key_title)));
		i.putExtra(Diary.key_content,c.getString(c.getColumnIndexOrThrow(Diary.key_content)));
		startActivityForResult(i,ACTIVITY_EDIT);
	}
    
}